let requestUrl;
let methodArray = ["get", "post"];
let remoteArray = ["普通接口", "远程接口"];
let typeArray = ["父权限", "子权限"];
let anonymityArray = ["不允许", "允许"];
/**
 * 页码
 */
let pageList = [15, 50 ,100, 200, 500];

$(document).ready(function () {

    $("#service_id").combobox({
        url: "/application/selectAll",
        valueField: "id",
        textField: "name",
        width: 150,
        prompt: "-选择应用-",
        panelHeight: "auto"
    });

    $("#parent_id").combobox({
        url: "/permission/selectParent",
        valueField: "id",
        textField: "name",
        width: 150,
        prompt: "-选择父权限-",
        panelHeight: "auto",
        groupField: "serviceId",
        groupFormatter: function(group) {
            ajaxGet("/application/selectById", {
                id: group
            }, function (resp) {
                let data = resp.data;

                $("#_service_" + group).html(data.name);
            }, error);

            return "<span class='blue' id='_service_" + group + "'></span>";
        }
    });

    $("#_remote_").combobox({
        data: getJsonData(remoteArray),
        valueField: "value",
        textField: "text",
        width: 150,
        prompt: "-选择接口类型-",
        panelHeight: "auto"
    });

    $("#search").linkbutton({
        iconCls: "icon-search"
    }).click(function() {
        let remote = $("#_remote_").combobox("getValue");
        let parentId = $("#parent_id").combobox("getValue");
        let serviceId = $("#service_id").combobox("getValue");

        $("#permission_list").datagrid("reload", {
            remote: remote,
            parentId: parentId,
            serviceId: serviceId
        });
    });

    $("#clear").linkbutton({
        iconCls: "icon-cancel"
    }).click(function() {
        $("#search_form").form("clear");
    });

    $("#id").textbox({
        width: 370,
        required: true
    });

    $("#name").textbox({
        width: 370,
        required: true
    });

    $("#value").textbox({
        width: 370,
        required: true
    });

    $("#url").textbox({
        width: 370,
        required: true
    });

    fetchServiceId();
    fetchSelect("#type", typeArray);
    fetchSelect("#remote", remoteArray);
    fetchSelect("#method", methodArray);
    fetchSelect("#anonymity", anonymityArray);

    $("#parentId").combobox({
        url: "/permission/selectParent",
        valueField: "id",
        textField: "name",
        width: 150,
        panelHeight: "auto",
        groupField: "serviceId",
        groupFormatter: function(group) {
            ajaxGet("/application/selectById", {
                id: group
            }, function (resp) {
                let data = resp.data;

                $("#service_" + group).html(data.name);
            }, error);

            return "<span class='blue' id='service_" + group + "'></span>";
        }
    });

    $("#permission_dialog").dialog({
        title: "权限信息",
        closed: true,
        modal: true,
        buttons: [{
            iconCls: "icon-save",
            text: "保存",
            handler: function() {
                // 验证表单
                let result = $("#permission_form").form("validate");

                if (result) {
                    // 获取表单数据
                    let data = $("#permission_form").serialize();

                    ajaxPost(requestUrl, data, function (resp) {
                        $("#permission_form").form("clear");
                        $("#permission_dialog").dialog("close");

                        // 重新加载表格数据
                        $("#permission_list").datagrid("reload");

                        $.messager.show({
                            title: "系统消息",
                            type: "slide",
                            msg: resp.message
                        });
                    }, error);
                } else {
                    $.messager.alert("系统提示", "请输入正确的表单项~", "warning");
                }
            }
        }, {
            iconCls: "icon-cancel",
            text: "取消",
            handler: function() {
                $("#permission_form").form("clear");
                $("#permission_dialog").dialog("close");
            }
        }]
    });

    $("#permission_list").datagrid({
        url: "/permission/selectByPage",
        height: 600,
        striped: true,
        rownumbers: true,
        singleSelect: true,
        selectOnCheck: false,
        pagination: true,
        pageList: pageList,
        pageSize: pageList[0],
        toolbar: [{
            iconCls: "icon-add",
            text: "添加",
            handler: function() {
                requestUrl = "/permission/insert";

                $("#permission_dialog").dialog("open");
            }
        }, "-", {
            iconCls: "icon-edit",
            text: "修改",
            handler: function() {
                // 获取选中的表格数据
                let rowData = $("#permission_list").datagrid("getSelected");

                if (rowData) {
                    requestUrl = "/permission/updateById";

                    $("#id").textbox("setValue", rowData.id);
                    $("#url").textbox("setValue", rowData.url);
                    $("#name").textbox("setValue", rowData.name);
                    $("#type").combobox("setValue", rowData.type);
                    $("#value").textbox("setValue", rowData.value);
                    $("#remote").combobox("setValue", rowData.remote);
                    $("#method").combobox("setValue", rowData.method);
                    $("#parentId").combobox("setValue", rowData.parentId);
                    $("#anonymity").combobox("setValue", rowData.anonymity);
                    $("#serviceId").combobox("setValue", rowData.serviceId);

                    $("#permission_dialog").dialog("open");
                } else {
                    $.messager.alert("系统提示", "请选择要修改的记录~", "warning");
                }
            }
        }, "-", {
            iconCls: "icon-cancel",
            text: "删除",
            handler: function() {
                // 获取勾选的表格数据
                let rows = $("#permission_list").datagrid("getChecked");

                let count = rows.length;

                if (count > 0) {
                    $.messager.confirm("系统提示", "确认删除勾选的" + count + "条数据？", function (bool) {
                        if (bool) {
                            let ids = [];

                            for (let i = 0; i < rows.length; i++) {
                                ids.push(rows[i].id);
                            }

                            ajaxPost("/permission/deleteByIds", {
                                ids: JSON.stringify(ids)
                            }, function (resp) {
                                $.messager.show({
                                    title: "系统消息",
                                    type: "slide",
                                    msg: resp.message
                                });

                                // 重新加载表格数据
                                $("#permission_list").datagrid("reload")
                            }, error);
                        }
                    });
                } else {
                    $.messager.alert("系统提示", "请选择要删除的记录！", "warning");
                }
            }
        }, "-", {
            iconCls: "icon-ok",
            text: "初始化",
            handler: function() {
                $.messager.confirm("系统提示", "该操作会删除本应用的权限，是否继续操作？", function (bool) {
                    if (bool) {
                        ajaxPost("/permission/init", {}, function (resp) {
                            $.messager.show({
                                title: "系统消息",
                                type: "slide",
                                msg: resp.message
                            });

                            $("#permission_list").datagrid("reload")
                        }, error);
                    }
                });
            }
        }],
        frozenColumns: [[
            {field: "ck", checkbox: true},
            {field: "id", title: "权限ID", width: 350, align: "center"},
            {field: "name", title: "权限名", width: 200, align: "center"}
        ]],
        columns: [[
            {field: "value", title: "权限值", width: 250, align: "center"},
            {field: "url", title: "请求路径", width: 250, align: "center"},
            {field: "type", title: "权限类型", width: 150, align: "center",
                formatter: function (value) {
                    return "<div>" + typeArray[value] + "</div>";
                }
            },
            {field: "method", title: "请求方式", width: 150, align: "center",
                formatter: function (value) {
                    return "<div>" + typeArray[value] + "</div>";
                }
            },
            {field: "anonymity", title: "匿名访问", width: 150, align: "center",
                formatter: function (value) {
                    return "<div>" + anonymityArray[value] + "</div>";
                }
            },
            {field: "parentId", title: "父级权限", width: 150, align: "center",
                formatter: function (value, rowData, rowIndex) {
                    if (value) {
                        ajaxGet("/permission/selectById", {
                            id: value
                        }, function (resp) {
                            let data = resp.data;

                            $("#parent_" + rowIndex).html(data.name);
                        }, error);

                        return "<div id='parent_" + rowIndex + "'></div>";
                    }

                    return "/";
                }
            },
            {field: "remote", title: "接口类型", width: 150, align: "center",
                formatter: function (value) {
                    return "<div>" + remoteArray[value] + "</div>";
                }
            },
            {field: "serviceId", title: "所属应用", width: 150, align: "center",
                formatter: function (value, rowData, rowIndex) {
                    ajaxGet("/application/selectById", {
                        id: value
                    }, function (resp) {
                        let data = resp.data;

                        $("#application_"+ rowIndex).attr({
                            "src": data.href,
                            "title": data.note
                        }).html(data.name);
                    }, error);

                    return "<a id='application_" + rowIndex + "'></a>";
                }
            }
        ]]
    });

});